package com.dao.user;

import com.model.po.ChartConfigList;
import com.model.po.Dashboards;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface RecordMapper {
    @Select("select count(*) from bi_record where bre_type = #{type} and bre_user_id = #{userId}")
    int getCount(@Param("type") String type, @Param("userId") int userId);

    /*
    插入
     */
    @Insert("insert into bi_record(bre_id, bre_type, bre_user_id, re_date, item_id) " +
            "values(#{id}, #{type}, #{userId}, to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss'), #{itemId})")
    @SelectKey(before=true,keyProperty="id",resultType=Integer.class,statement="SELECT BI_RECORD_SEQUENCE.nextval from dual",keyColumn = "bre_id")
    void inputRecord(@Param("type") String type, @Param("userId") int userId, @Param("itemId") int itemId);

    /*
    删除
     */
    @Delete("delete from bi_record where bre_id in (select bre_id from" +
            "(" +
            "select * from BI_RECORD where bre_type = #{type} and bre_user_id = #{userId} order by RE_DATE asc" +
            ") where rownum <= 1)")
    void deleteRecord(@Param("type") String type, @Param("userId") int userId);

    /*
    查询最新List数据
     */
    @Select("select item_id from BI_RECORD where bre_type = #{type} and bre_user_id = #{userId} order by RE_DATE desc")
    List<Integer> getListId(@Param("type") String type, @Param("userId") int userId);

    /*
     更新
      */
    @Update("update bi_record set re_date = to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss') " +
            "where bre_type = #{type} and item_id = #{itemId}  ")
    void updateRecord(@Param("type") String type, @Param("itemId") int itemId);

    /*
    查询图表10条纪录
     */
    @Select("select id as chartId, chart_name as chartName, chart_type as chartType, create_by as createBy, create_date as createDate, BC_FILTERS as filters, re_date as reDate" +
            "  ,bc_filters as filters, CHART_DESCRIBES as describes, bc_charts_group as chartsGroup, bc_charts_option as chartOption ,bd_data_id as dataId, create_id as createId " +
            "from bi_charts left join BI_RECORD on id = item_id where bre_type = '0' and " +
            "bre_user_id = #{userId} order by RE_DATE desc")
    List<ChartConfigList> chartsRecode(int userId);

    /*
    查询看板10条纪录
     */
    @Select("select id, bd_note as bdNote, bd_name as bdName, CONFIGURATION as bdConfiguration, CREATE_BY as createBy, create_id as createId, re_date as reDate, " +
            "CREATE_DATE as createDate, BD_THUMBNAIL as thumbnail from BI_DASHBOARDS left join " +
            "BI_RECORD on id = item_id where bre_type = '1' and " +
            "bre_user_id = #{userId} order by RE_DATE desc")
    List<Dashboards> DashRecode(int userId);

}
